﻿using CommonTools;
using CommonTools.Controls;
using shop.Bll.cost;
using shop.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;

namespace shop.cost
{
    /// <summary>  </summary>
    public class GoodsOperatorBllUI : GoodsOperatorBll
    {
        private ControlBuilder _builder;

        public override ControlBuilder GetUIBuilder()
        {
            _builder = base.GetUIBuilder()
                .SetFlowBreak()
                .AddButton("导入", ImportData, false)
                .AddButton("下载模板", templateDownload, false);

            return _builder;
        }

        /// <summary> 导入 </summary>
        public async Task<PageButtonResult> ImportData()
        {
            var dt = ExcelHelper.Import();
            if(dt == null) 
            {
                return new PageButtonResult(false,"没有导入数据", false);
            }

            //
            //var batch = DateTime.Now.ToString("yyyy-MM-ddTHHmmss")+ Guid.NewGuid().ToString();

            var res = await WaitUI.RunAsync(async () =>
            {
                var list = dt.AsEnumerable()
                 .Select(a => new GoodsOperator()
                 {
                     platform_goods_id = a.Field<string>(0),
                     Operator = a.Field<string>(1),
                 })
                 .Where(a => string.IsNullOrEmpty(a.platform_goods_id) == false || string.IsNullOrEmpty(a.Operator) == false )
                 .ToList();

                var res1 = await base.SaveItem(list);
      
                return res1;
            });

            if (res.IsOk)
            {
                //修改旧的日期  a是旧数据

                return new PageButtonResult(true, "导入完成", true);
            }
            else
            {
                return new PageButtonResult(false, res.Message);
                //MessageBox.Show(res.Message, Properties.Resources.systemInformation, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        /// <summary>  </summary>
        public async Task<PageButtonResult> templateDownload()
        {
            var list = (new int[0])
                .Select(a=> new
                {
                    货品平台ID = string.Empty,
                    运营负责人 = string.Empty,
                })
                .ToList();

            new ExcelExporter()
                .NewSheet(null)
                .AddList(list)
                .EndThenOpen("运营负责人模板");


            return await Task.FromResult(new PageButtonResult(false));
        }


    }
}
